package com.jz.community.moduleshopping.orderDetail.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.LoginBaseInfo;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshopping.orderDetail.bean.EvaluateInfo;

import java.util.HashMap;

/**
 * Created by Target on 2018/4/6.
 */

public class OrderEvaluateDetailTask extends RxTask<String, Integer, EvaluateInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public OrderEvaluateDetailTask(Activity activity,
                                   ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected EvaluateInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Constant.ORDER_COMMONIT, getParam(params[0], params[1], params[2], params[3], params[4]));
        EvaluateInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, EvaluateInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(EvaluateInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam(String orderId, String orderCommentContent, String orderGoodsComment, String serviceAttitude, String shopId) {
        LoginBaseInfo loginBaseInfo = BaseSpUtils.getInstance().getUserBaseInfo(activity);
        HashMap<String, Object> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("orderCommentContent", orderCommentContent);
        params.put("orderGoodsComment", orderGoodsComment);
        params.put("serviceAttitude", serviceAttitude);
        params.put("userId", BaseSpUtils.getInstance().getUserId(activity));
        params.put("shopId", shopId);
        params.put("userName", loginBaseInfo.getName());
        return params;
    }
}
